// pages/result/result.js
var app = getApp();
const setting = require("../../http/env");
const httpApi = require("../../http/request"); //后续可以改为你自己的域名接口地址

Page({
  /**
   * 页面的初始数据
   */
  data: {
    //默认域名
    baseUrl: setting.baseUrl,
    //答题比赛id
    quest_game_id: 0,

    //数据
    list: {},

    //显示自由提示
    showCheer: false,
    //显示翻倍提示
    showDouble: false,
    userInfo: {},

    // 剩余比赛次数
    count: null,
    showBaseUrl: false,
    // 次数用尽提示
    showRemind: false,
    // 是否能继续跟TA对战 0-是 1-否
    continue_flag: 0,
    showGroup: false, // 显示btn组，从伴读、与书加跳转过来显示
  },

  //获取结果数据
  getResult(e) {
    var that = this;
    var param = {
      user_id: that.data.user.id,
      quest_game_id: that.data.quest_game_id,
    };
    var user = wx.getStorageSync("userInfo");
    httpApi.Post("/index/comGame", param).then((res) => {
      var showDouble = false;
      if (res.data.score_double > 1) {
        showDouble = true;
      }

      if (res.data.initiate_user_id == user.id) { // 自己是邀请人
        res.data.is_success = res.data.initiate_user.sorce > res.data.match_user.sorce ? 1 : res.data.initiate_user.sorce < res.data.match_user.sorce ? 0 : 2;
      } else { // 自己是被邀请人
        res.data.is_success = res.data.initiate_user.sorce > res.data.match_user.sorce ? 0 : res.data.initiate_user.sorce < res.data.match_user.sorce ? 1 : 2;
      }
      that.setData({
        list: res.data, 
        showDouble: showDouble,
        showBaseUrl: !res.data?.initiate_user?.headimg?.includes('http')
      });
      setTimeout(() => {
        this.setData({
          showDouble: false,
        });
      }, 4000);
    });
  },

  // 跳转到知识图谱
  toKonwledgePage() {
    wx.navigateTo({
      url: "/pages/knowledge-new/knowledge-new",
    });
  },

  //自由匹配再来一局
  startGame(e) {
    wx.redirectTo({
      url: "/pages/pk-loading/pk-loading",
    });
  },

  //好友匹配再来一局
  frendGame(e){
    if (this.data.continue_flag == 1) {
      this.setData({
        showRemind: true
      })
      return;
    }
    var that=this;
    wx.redirectTo({
      url: "/pages/friendVs/friendVs?quest_game_id="+that.data.list.id+'&nedd_back=1',
    });
  },

  frendGameOne(e){
    wx.redirectTo({
      url: "/pages/friendVs/friendVs?nedd_back=1",
    });
  },

  //退出提示
  back() {
    var that=this;
    console.log("exit");
    if(that.data.list.tis){
      that.setData({
        showCheer: true,
      });
    }else{
      wx.redirectTo({
        url: "../home/home",
      });
    }
  },

  //关闭翻倍
  closeDb(e) {
    var that = this;
    that.setData({
      showDouble: false,
    });
  },

  //继续答题
  closeChe(e) {
    var that = this;
    that.setData({
      showCheer: false,
    });
    if(that.data.list.type==1){
      this.startGame();
    }else{
      this.frendGame();
    }
  },

  getUserInfoByApi() {
    var that = this;
    var user = wx.getStorageSync("userInfo");
    httpApi.Post("/index/getUser", { user_id: user.id }).then((res) => {
      var user = wx.getStorageSync("userInfo") || {};
      wx.setStorageSync("userInfo", Object.assign({}, user, res.data));
      that.setData({
        userInfo: res.data,
      });
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;
    //答题比赛id
    var quest_game_id = options.quest_game_id ? options.quest_game_id : 0;
    //用户数据
    var user = wx.getStorageSync("userInfo");
    //参数判断
    if(!user || !quest_game_id){
      wx.redirectTo({
        url: '../home/home',
      })
    }

    //保存数据
    that.setData({
      user: user,
      quest_game_id: quest_game_id,
      count: Number(options.robot_flag) ? options.count : null,
      continue_flag: options.continue_flag,
      showGroup: user.limit_count_flag == 0
    });
    
    //获取结果
    that.getResult();
    that.getUserInfoByApi();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage(e) {
    var user=wx.getStorageSync("userInfo");
    var path='/pages/home/home?unitid='+user.unitid+'&unitid_copy='+user.unitid_copy + "&inviterId=" + wx.getStorageSync("userInfo").id;
    return {
      title: app.globalData.shareInfo[e.from].title,
      imageUrl: app.globalData.shareInfo[e.from].imageUrl,
      path: path,
      success: function(res) {
        // 转发成功
        console.log(11212);
      },
      fail: function(res) {
        // 转发失败
      }
    }
  },
});
